from typing import ItemsView
import grpc
import grpcDemo_pb2
import grpcDemo_pb2_grpc
from concurrent import futures
from io import BytesIO
import numpy as np
import shapely
from shapely.geometry import Polygon
import matplotlib.pyplot as plt

coordinate_beta_x = [29.25, 51.45, 68.91, 87.86, 70.95, 54.36, 35.16, 23]
coordinate_beta_y = [190.46, 214.12, 249.74, 215.84, 60.51, 26.57, 52.97, 156.18]

class PyServer(grpcDemo_pb2_grpc.GrpcDemoServicer):


    def GetMessgae(self,requet,context):
        pressure_arrays = np.random.uniform(low=0.0, high=20.0,size=(10, 8))
        nda_bytes = BytesIO()
        np.save(nda_bytes, pressure_arrays, allow_pickle=False)
        print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
        print(pressure_arrays)
        print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
        return grpcDemo_pb2.Response(arrays=nda_bytes.getvalue())
    def SetUserInfo(self,requet,context): 
        print("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB")
        print(requet.listArrays) 

        print("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB")
        print("data_listdata_listdata_listdata_listdata_listdata_listdata_list")
        data_list = [[]] * (len(requet.listArrays))
        j = 0 
        for gray_feature in requet.listArrays:  
            item = gray_feature.arrays 
            data_list[j]=item
            j = j+1
            print("j",j)
        """
        for gray_feature in requet.listArrays: 
           j = 0
           feature = gray_feature.arrays
           length = len(feature)
           matrix = [] * length
           for i in range(0, length):
               matrix[i] = feature[i].arrays
           data_list[j] = matrix
           j += 1
        """
        """
        """
        print(data_list)
        center_array = []
        for pressure in data_list:
           Gx = np.multiply(np.array(pressure),np.array(coordinate_beta_x)).sum() / np.array(pressure).sum()
           Gy = np.multiply(np.array(pressure),np.array(coordinate_beta_y)).sum() / np.array(pressure).sum()
           center_array.append([Gx, Gy])
        print("center_array",center_array)
       
        """
        """
        if "./left_center.png" is not None:
             png_path = "./left.png"  
             img=plt.imread(png_path)
             plt.imshow(img, extent=[0,110,0,280])
             plt.scatter(np.array(center_array)[:, 0],np.array(center_array)[:, 1], marker='o', c='y')
        plt.savefig("./left_center.png")
        """
        """

        from PIL import Image 
        from io import BytesIO
        import base64


        figfile = BytesIO() 
        plt.savefig(figfile, format='png')
        figfile.seek(0)  # rewind to beginning of file
        figdata_png = base64.b64encode(figfile.getvalue())
        img_str = str(figdata_png, "utf-8")

        print("data_listdata_listdata_listdata_listdata_listdata_listdata_list")
        return grpcDemo_pb2.SetResponse(info="成功",imgBase64=img_str)
def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    grpcDemo_pb2_grpc.add_GrpcDemoServicer_to_server(PyServer(),server)
    server.add_insecure_port('127.0.0.1:5055')
    server.start()
    server.wait_for_termination()


if __name__ == '__main__':
    #logging.basicConfig()
    serve()